package com.android.love.ui;

import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.love.R;
import com.android.love.net.UriHelper;
import com.android.love.util.ActivityUtil;
import com.android.love.util.DeviceUtil;
import com.android.love.util.NetworkUtil;
import com.android.love.vo.MyRequest;
import com.android.love.vo.UserInfo;
import com.igexin.slavesdk.MessageManager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class SplashActivity extends BasePageActivity {

	private UserInfo info;
	private long startTime;
	private int count;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_logo);
		MessageManager.getInstance().initialize(this.getApplicationContext());
		if (!NetworkUtil.isAvailable(context)) {
			ActivityUtil.show(SplashActivity.this, "无网络");
			baseHandler.postDelayed(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					finish();
				}
			}, 2000);
			return;
		}
		if (user_id != 0 && !"".equals(sig)) {
			// 已有用户信息
			// startHome(3000);
			startTime = System.currentTimeMillis();
			MyRequest req = new MyRequest();
			req.authority = UriHelper.REALM_NAME;
			req.path = UriHelper.URL_ORDER_PAYCOUNT;
			req.paramers = generateOrderParams();
			getDataFromTask(context, NOT_PAY_NUM, req, count, false, false);
		} else {
			// 注册用户信息
			findViewById(R.id.tv_logo).setVisibility(View.VISIBLE);
			findViewById(R.id.progressBar).setVisibility(View.VISIBLE);
			getData();
		}
	}

	private void startHome(long delaytime) {
		baseHandler.postDelayed(new Runnable() {

			@Override
			public void run() {
				findViewById(R.id.tv_logo).setVisibility(View.GONE);
				findViewById(R.id.progressBar).setVisibility(View.GONE);
				Intent intent = new Intent(context, HomeActivity.class);
				startActivity(intent);
				overridePendingTransition(android.R.anim.fade_in,
						android.R.anim.fade_out);
				 finish();
			}
		}, delaytime);
	}

	@Override
	protected Boolean loadBottomTab() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	protected void getData() {
		super.getData();
		startTime = System.currentTimeMillis();
		MyRequest req = new MyRequest();
		req.authority = UriHelper.REALM_NAME;
		req.path = UriHelper.URL_REGISTER;
		req.paramers = generateParams();
		getDataFromTask(context, GET_USER_INFO, req, info, false, false);
	}

	/**
	 * 得到传递的参数
	 * 
	 * @return
	 * @throws JSONException
	 * @throws IOException
	 */
	public Map<String, String> generateParams() {
		TreeMap<String, String> params = new TreeMap<String, String>();
		params.put("app_key", "android");
		params.put("v", "1.0");
		params.put("cid", "");
		params.put("phone_key", DeviceUtil.getMac(context));
		params.put("open_udid", DeviceUtil.getImei(context));
		params.put("phone_agent", DeviceUtil.getModel(context) + " "
				+ DeviceUtil.getOSversion(context));
		return params;
	}

	/**
	 * 得到传递的参数
	 * 
	 * @return
	 * @throws JSONException
	 * @throws IOException
	 */
	public Map<String, String> generateOrderParams() {
		TreeMap<String, String> params = new TreeMap<String, String>();
		params.put("app_key", "android");
		params.put("v", "1.0");
		if (sp.getValue(USER_ID, 0) != 0)
			params.put("user_id", sp.getValue(USER_ID, 0) + "");
		if (!"".equals(sp.getValue(SIG, "")))
			params.put("sig", sp.getValue(SIG, ""));
		return params;
	}

	@Override
	public void handle(int tag, Object object) {
		super.handle(tag, object);
		switch (tag) {
		case GET_USER_INFO:
			if (object != null) {
				// ActivityUtil.show(SplashActivity.this, "注册成功");
				info = (UserInfo) object;
				sp.setValue(USER_ID, info.getUser_id());
				sp.setValue(SIG, info.getSig());
				long time = System.currentTimeMillis() - startTime;
				if (time >= 3000) {
					// 直接进入主页
					startHome(0);
				} else {
					// 延迟进入主页
					startHome(3000 - time);
				}
			}
			break;
		case NOT_PAY_NUM:
			if (object != null) {
				count = (Integer) object;
				if (count > 0) {
					sp.setValue(ORDER_NUM, count);
				}
			}
			long time = System.currentTimeMillis() - startTime;
			if (time >= 3000) {
				// 直接进入主页
				startHome(0);
			} else {
				// 延迟进入主页
				startHome(3000 - time);
			}
			break;

		default:
			break;
		}
	}

}
